USE [SmartParking]

SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO

------------------------------------
-- TABLE: Customer
------------------------------------
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spCustomer_SA]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spCustomer_SA]
GO

CREATE PROCEDURE [dbo].[spCustomer_SA]
AS
BEGIN
	SELECT *
	FROM [Customer]
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spCustomer_SxId]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spCustomer_SxId]
GO

CREATE PROCEDURE [dbo].[spCustomer_SxId]
	@IdCustomer int
AS
BEGIN
	SELECT *
	FROM [Customer]
	WHERE [IdCustomer] = @IdCustomer
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spCustomer_I]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spCustomer_I]
GO

CREATE PROCEDURE [dbo].[spCustomer_I]
	@IdCustomer int output,
	@Name nchar(50),
	@Email nchar(75),
	@Phone nchar(12),
	@Address nchar(100)
AS
BEGIN
	INSERT INTO [Customer] ([Name], [Email], [Phone], [Address])
	VALUES(@Name, @Email, @Phone, @Address)
	SET @IdCustomer = @@IDENTITY
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spCustomer_U]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spCustomer_U]
GO

CREATE PROCEDURE [dbo].[spCustomer_U]
	@IdCustomer int,
	@Name nchar(50),
	@Email nchar(75),
	@Phone nchar(12),
	@Address nchar(100)
AS
BEGIN
	UPDATE [Customer]
	SET [Name] = @Name, [Email] = @Email, [Phone] = @Phone, [Address] = @Address
	WHERE [IdCustomer] = @IdCustomer
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spCustomer_D]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spCustomer_D]
GO

CREATE PROCEDURE [dbo].[spCustomer_D]
	@IdCustomer int
AS
BEGIN
	DELETE [Customer]
	WHERE [IdCustomer] = @IdCustomer
END
GO

------------------------------------
-- TABLE: Permission
------------------------------------
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spPermission_SA]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spPermission_SA]
GO

CREATE PROCEDURE [dbo].[spPermission_SA]
AS
BEGIN
	SELECT *
	FROM [Permission]
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spPermission_SxId]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spPermission_SxId]
GO

CREATE PROCEDURE [dbo].[spPermission_SxId]
	@IdPermission int
AS
BEGIN
	SELECT *
	FROM [Permission]
	WHERE [IdPermission] = @IdPermission
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spPermission_I]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spPermission_I]
GO

CREATE PROCEDURE [dbo].[spPermission_I]
	@IdPermission int output,
	@Name nchar(50)
AS
BEGIN
	INSERT INTO [Permission] ([Name])
	VALUES(@Name)
	SET @IdPermission = @@IDENTITY
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spPermission_U]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spPermission_U]
GO

CREATE PROCEDURE [dbo].[spPermission_U]
	@IdPermission int,
	@Name nchar(50)
AS
BEGIN
	UPDATE [Permission]
	SET [Name] = @Name
	WHERE [IdPermission] = @IdPermission
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spPermission_D]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spPermission_D]
GO

CREATE PROCEDURE [dbo].[spPermission_D]
	@IdPermission int
AS
BEGIN
	DELETE [Permission]
	WHERE [IdPermission] = @IdPermission
END
GO

------------------------------------
-- TABLE: Role
------------------------------------
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spRole_SA]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spRole_SA]
GO

CREATE PROCEDURE [dbo].[spRole_SA]
AS
BEGIN
	SELECT *
	FROM [Role]
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spRole_SxId]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spRole_SxId]
GO

CREATE PROCEDURE [dbo].[spRole_SxId]
	@IdRole int
AS
BEGIN
	SELECT *
	FROM [Role]
	WHERE [IdRole] = @IdRole
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spRole_I]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spRole_I]
GO

CREATE PROCEDURE [dbo].[spRole_I]
	@IdRole int output,
	@Name nchar(25)
AS
BEGIN
	INSERT INTO [Role] ([Name])
	VALUES(@Name)
	SET @IdRole = @@IDENTITY
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spRole_U]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spRole_U]
GO

CREATE PROCEDURE [dbo].[spRole_U]
	@IdRole int,
	@Name nchar(25)
AS
BEGIN
	UPDATE [Role]
	SET [Name] = @Name
	WHERE [IdRole] = @IdRole
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spRole_D]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spRole_D]
GO

CREATE PROCEDURE [dbo].[spRole_D]
	@IdRole int
AS
BEGIN
	DELETE [Role]
	WHERE [IdRole] = @IdRole
END
GO

------------------------------------
-- TABLE: RolePermission
------------------------------------
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spRolePermission_SA]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spRolePermission_SA]
GO

CREATE PROCEDURE [dbo].[spRolePermission_SA]
AS
BEGIN
	SELECT *
	FROM [RolePermission]
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spRolePermission_SxId]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spRolePermission_SxId]
GO

CREATE PROCEDURE [dbo].[spRolePermission_SxId]
	@IdRole int,
	@IdPermission int
AS
BEGIN
	SELECT *
	FROM [RolePermission]
	WHERE [IdRole] = @IdRole AND [IdPermission] = @IdPermission
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spRolePermission_SxRole]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spRolePermission_SxRole]
GO

CREATE PROCEDURE [dbo].[spRolePermission_SxRole]
	@IdRole int
AS
BEGIN
	SELECT *
	FROM [RolePermission]
	WHERE [IdRole] = @IdRole
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spRolePermission_I]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spRolePermission_I]
GO

CREATE PROCEDURE [dbo].[spRolePermission_I]
	@IdRole int,
	@IdPermission int
AS
BEGIN
	INSERT INTO [RolePermission] ([IdRole], [IdPermission])
	VALUES(@IdRole, @IdPermission)
	
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spRolePermission_U]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spRolePermission_U]
GO

CREATE PROCEDURE [dbo].[spRolePermission_U]
	@IdRole int,
	@IdPermission int
AS
BEGIN
	UPDATE [RolePermission]
	SET [IdRole] = @IdRole, [IdPermission] = @IdPermission
	WHERE [IdRole] = @IdRole AND [IdPermission] = @IdPermission
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spRolePermission_D]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spRolePermission_D]
GO

CREATE PROCEDURE [dbo].[spRolePermission_D]
	@IdRole int,
	@IdPermission int
AS
BEGIN
	DELETE [RolePermission]
	WHERE [IdRole] = @IdRole AND [IdPermission] = @IdPermission
END
GO

------------------------------------
-- TABLE: Manager
------------------------------------
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spManager_SA]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spManager_SA]
GO

CREATE PROCEDURE [dbo].[spManager_SA]
AS
BEGIN
	SELECT *
	FROM [Manager]
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spManager_SxId]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spManager_SxId]
GO

CREATE PROCEDURE [dbo].[spManager_SxId]
	@IdManager int
AS
BEGIN
	SELECT *
	FROM [Manager]
	WHERE [IdManager] = @IdManager
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spManager_SxUserPassword]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spManager_SxUserPassword]
GO

CREATE PROCEDURE [dbo].[spManager_SxUserPassword]
	@User nchar(15),
	@Password nchar(12)
AS
BEGIN
	SELECT *
	FROM [Manager]
	WHERE [User] = @User
	AND [Password] = @Password
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spManager_I]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spManager_I]
GO

CREATE PROCEDURE [dbo].[spManager_I]
	@IdManager int output,
	@IdCustomer int,
	@IdRole int,
	@User nchar(15),
	@Password nchar(12),
	@Email nchar(75),
	@Phone nchar(12)
AS
BEGIN
	INSERT INTO [Manager] ([IdCustomer], [IdRole], [User], [Password], [Email], [Phone])
	VALUES(@IdCustomer, @IdRole, @User, @Password, @Email, @Phone)
	SET @IdManager = @@IDENTITY
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spManager_U]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spManager_U]
GO

CREATE PROCEDURE [dbo].[spManager_U]
	@IdManager int,
	@IdCustomer int,
	@IdRole int,
	@User nchar(15),
	@Password nchar(12),
	@Email nchar(75),
	@Phone nchar(12)
AS
BEGIN
	UPDATE [Manager]
	SET [IdCustomer] = @IdCustomer, [IdRole] = @IdRole, [User] = @User, [Password] = @Password, [Email] = @Email, [Phone] = @Phone
	WHERE [IdManager] = @IdManager
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spManager_D]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spManager_D]
GO

CREATE PROCEDURE [dbo].[spManager_D]
	@IdManager int
AS
BEGIN
	DELETE [Manager]
	WHERE [IdManager] = @IdManager
END
GO

------------------------------------
-- TABLE: Log
------------------------------------
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spLog_SA]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spLog_SA]
GO

CREATE PROCEDURE [dbo].[spLog_SA]
AS
BEGIN
	SELECT *
	FROM [Log]
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spLog_SxId]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spLog_SxId]
GO

CREATE PROCEDURE [dbo].[spLog_SxId]
	@IdLog bigint
AS
BEGIN
	SELECT *
	FROM [Log]
	WHERE [IdLog] = @IdLog
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spLog_I]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spLog_I]
GO

CREATE PROCEDURE [dbo].[spLog_I]
	@IdLog bigint output,
	@IdManager int,
	@Action nchar(75),
	@Table nchar(30),
	@AffectedId nchar(15),
	@IPAddress nchar(15),
	@Date datetime
AS
BEGIN
	INSERT INTO [Log] ([IdManager], [Action], [Table], [AffectedId], [IPAddress], [Date])
	VALUES(@IdManager, @Action, @Table, @AffectedId, @IPAddress, @Date)
	SET @IdLog = @@IDENTITY
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spLog_U]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spLog_U]
GO

CREATE PROCEDURE [dbo].[spLog_U]
	@IdLog bigint,
	@IdManager int,
	@Action nchar(75),
	@Table nchar(30),
	@AffectedId nchar(15),
	@IPAddress nchar(15),
	@Date datetime
AS
BEGIN
	UPDATE [Log]
	SET [IdManager] = @IdManager, [Action] = @Action, [Table] = @Table, [AffectedId] = @AffectedId, [IPAddress] = @IPAddress, [Date] = @Date
	WHERE [IdLog] = @IdLog
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spLog_D]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spLog_D]
GO

CREATE PROCEDURE [dbo].[spLog_D]
	@IdLog bigint
AS
BEGIN
	DELETE [Log]
	WHERE [IdLog] = @IdLog
END
GO

------------------------------------
-- TABLE: Park
------------------------------------
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spPark_SA]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spPark_SA]
GO

CREATE PROCEDURE [dbo].[spPark_SA]
AS
BEGIN
	SELECT *
	FROM [Park]
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spPark_SxId]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spPark_SxId]
GO

CREATE PROCEDURE [dbo].[spPark_SxId]
	@IdPark int
AS
BEGIN
	SELECT *
	FROM [Park]
	WHERE [IdPark] = @IdPark
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spPark_SxCustomer]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spPark_SxCustomer]
GO

CREATE PROCEDURE [dbo].[spPark_SxCustomer]
	@IdCustomer int
AS
BEGIN
	SELECT *
	FROM [Park]
	WHERE [IdCustomer] = @IdCustomer
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spPark_I]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spPark_I]
GO

CREATE PROCEDURE [dbo].[spPark_I]
	@IdPark int output,
	@IdCustomer int,
	@Name nchar(50),
	@BankAccountNumber nchar(30)
AS
BEGIN
	INSERT INTO [Park] ([IdCustomer], [Name], [BankAccountNumber])
	VALUES(@IdCustomer, @Name, @BankAccountNumber)
	SET @IdPark = @@IDENTITY
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spPark_U]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spPark_U]
GO

CREATE PROCEDURE [dbo].[spPark_U]
	@IdPark int,
	@IdCustomer int,
	@Name nchar(50),
	@BankAccountNumber nchar(30)
AS
BEGIN
	UPDATE [Park]
	SET [IdCustomer] = @IdCustomer, [Name] = @Name, [BankAccountNumber] = @BankAccountNumber
	WHERE [IdPark] = @IdPark
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spPark_D]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spPark_D]
GO

CREATE PROCEDURE [dbo].[spPark_D]
	@IdPark int
AS
BEGIN
	DELETE [Park]
	WHERE [IdPark] = @IdPark
END
GO

------------------------------------
-- TABLE: ParkZoneSpace
------------------------------------
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spParkZoneSpace_SA]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spParkZoneSpace_SA]
GO

CREATE PROCEDURE [dbo].[spParkZoneSpace_SA]
AS
BEGIN
	SELECT *
	FROM [ParkZoneSpace]
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spParkZoneSpace_SxId]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spParkZoneSpace_SxId]
GO

CREATE PROCEDURE [dbo].[spParkZoneSpace_SxId]
	@IdParkZone int,
	@IdParkZoneSpace int
AS
BEGIN
	SELECT *
	FROM [ParkZoneSpace]
	WHERE [IdParkZone] = @IdParkZone AND [IdParkZoneSpace] = @IdParkZoneSpace
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spParkZoneSpace_I]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spParkZoneSpace_I]
GO

CREATE PROCEDURE [dbo].[spParkZoneSpace_I]
	@IdParkZone int,
	@IdParkZoneSpace int
AS
BEGIN
	INSERT INTO [ParkZoneSpace] ([IdParkZone], [IdParkZoneSpace])
	VALUES(@IdParkZone, @IdParkZoneSpace)
	
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spParkZoneSpace_U]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spParkZoneSpace_U]
GO

CREATE PROCEDURE [dbo].[spParkZoneSpace_U]
	@IdParkZone int,
	@IdParkZoneSpace int
AS
BEGIN
	UPDATE [ParkZoneSpace]
	SET [IdParkZone] = @IdParkZone, [IdParkZoneSpace] = @IdParkZoneSpace
	WHERE [IdParkZone] = @IdParkZone AND [IdParkZoneSpace] = @IdParkZoneSpace
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spParkZoneSpace_D]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spParkZoneSpace_D]
GO

CREATE PROCEDURE [dbo].[spParkZoneSpace_D]
	@IdParkZone int,
	@IdParkZoneSpace int
AS
BEGIN
	DELETE [ParkZoneSpace]
	WHERE [IdParkZone] = @IdParkZone AND [IdParkZoneSpace] = @IdParkZoneSpace
END
GO

------------------------------------
-- TABLE: ParkZone
------------------------------------
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spParkZone_SA]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spParkZone_SA]
GO

CREATE PROCEDURE [dbo].[spParkZone_SA]
AS
BEGIN
	SELECT *
	FROM [ParkZone]
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spParkZone_SxId]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spParkZone_SxId]
GO

CREATE PROCEDURE [dbo].[spParkZone_SxId]
	@IdParkZone int
AS
BEGIN
	SELECT *
	FROM [ParkZone]
	WHERE [IdParkZone] = @IdParkZone
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spParkZone_SxIdPark]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spParkZone_SxIdPark]
GO

CREATE PROCEDURE [dbo].[spParkZone_SxIdPark]
	@IdPark int
AS
BEGIN
	SELECT *
	FROM [ParkZone]
	WHERE [IdPark] = @IdPark
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spParkZone_I]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spParkZone_I]
GO

CREATE PROCEDURE [dbo].[spParkZone_I]
	@IdPark int,
	@IdParkZone int output,
	@Name nchar(30),
	@PriceHour decimal,
	@ManagerCommisionAmount decimal,
	@WebCommisionAmount decimal,
	@ReserveAmount decimal,
	@AllowHalfHour nchar(1),
	@OpenHour nchar(6),
	@CloseHour nchar(6),
	@RegularSpaces int,
	@SpacesToReserve int
AS
BEGIN
	INSERT INTO [ParkZone] ([IdPark], [Name], [PriceHour], [ManagerCommisionAmount], [WebCommisionAmount], [ReserveAmount], [AllowHalfHour], [OpenHour], [CloseHour], [RegularSpaces], [SpacesToReserve])
	VALUES(@IdPark, @Name, @PriceHour, @ManagerCommisionAmount, @WebCommisionAmount, @ReserveAmount, @AllowHalfHour, @OpenHour, @CloseHour, @RegularSpaces, @SpacesToReserve)
	SET @IdParkZone = @@IDENTITY
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spParkZone_U]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spParkZone_U]
GO

CREATE PROCEDURE [dbo].[spParkZone_U]
	@IdPark int,
	@IdParkZone int,
	@Name nchar(30),
	@PriceHour decimal,
	@ManagerCommisionAmount decimal,
	@WebCommisionAmount decimal,
	@ReserveAmount decimal,
	@AllowHalfHour nchar(1),
	@OpenHour nchar(6),
	@CloseHour nchar(6),
	@RegularSpaces int,
	@SpacesToReserve int
AS
BEGIN
	UPDATE [ParkZone]
	SET [IdPark] = @IdPark, [Name] = @Name, [PriceHour] = @PriceHour, [ManagerCommisionAmount] = @ManagerCommisionAmount, [WebCommisionAmount] = @WebCommisionAmount, [ReserveAmount] = @ReserveAmount, [AllowHalfHour] = @AllowHalfHour, [RegularSpaces] = @RegularSpaces, [SpacesToReserve] = @SpacesToReserve
	WHERE [IdParkZone] = @IdParkZone
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spParkZone_D]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spParkZone_D]
GO

CREATE PROCEDURE [dbo].[spParkZone_D]
	@IdParkZone int
AS
BEGIN
	DELETE [ParkZone]
	WHERE [IdParkZone] = @IdParkZone
END
GO

------------------------------------
-- TABLE: ManagerParkZone
------------------------------------
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spManagerParkZone_SA]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spManagerParkZone_SA]
GO

CREATE PROCEDURE [dbo].[spManagerParkZone_SA]
AS
BEGIN
	SELECT *
	FROM [ManagerParkZone]
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spManagerParkZone_SxId]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spManagerParkZone_SxId]
GO

CREATE PROCEDURE [dbo].[spManagerParkZone_SxId]
	@IdManager int,
	@IdPark int,
	@IdParkZone int
AS
BEGIN
	SELECT *
	FROM [ManagerParkZone]
	WHERE [IdManager] = @IdManager AND [IdPark] = @IdPark AND [IdParkZone] = @IdParkZone
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spManagerParkZone_SxIdManager]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spManagerParkZone_SxIdManager]
GO

CREATE PROCEDURE [dbo].[spManagerParkZone_SxIdManager]
	@IdManager int
AS
BEGIN
	SELECT *
	FROM [ManagerParkZone]
	WHERE [IdManager] = @IdManager
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spManagerParkZone_SxIdParkZone]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spManagerParkZone_SxIdParkZone]
GO

CREATE PROCEDURE [dbo].[spManagerParkZone_SxIdParkZone]
	@IdParkZone int
AS
BEGIN
	SELECT *
	FROM [ManagerParkZone]
	WHERE [IdParkZone] = @IdParkZone
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spManagerParkZone_I]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spManagerParkZone_I]
GO

CREATE PROCEDURE [dbo].[spManagerParkZone_I]
	@IdManager int,
	@IdPark int,
	@IdParkZone int
AS
BEGIN
	INSERT INTO [ManagerParkZone] ([IdManager], [IdPark], [IdParkZone])
	VALUES(@IdManager, @IdPark, @IdParkZone)
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spManagerParkZone_U]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spManagerParkZone_U]
GO

CREATE PROCEDURE [dbo].[spManagerParkZone_U]
	@IdManager int,
	@IdPark int,
	@IdParkZone int
AS
BEGIN
	UPDATE [ManagerParkZone]
	SET [IdManager] = @IdManager, [IdPark] = @IdPark, [IdParkZone] = @IdParkZone
	WHERE [IdManager] = @IdManager AND [IdPark] = @IdPark AND [IdParkZone] = @IdParkZone
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spManagerParkZone_D]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spManagerParkZone_D]
GO

CREATE PROCEDURE [dbo].[spManagerParkZone_D]
	@IdManager int,
	@IdPark int,
	@IdParkZone int
AS
BEGIN
	DELETE [ManagerParkZone]
	WHERE [IdManager] = @IdManager AND [IdPark] = @IdPark AND [IdParkZone] = @IdParkZone
END
GO

------------------------------------
-- TABLE: Transaction
------------------------------------
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spTransaction_SA]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spTransaction_SA]
GO

CREATE PROCEDURE [dbo].[spTransaction_SA]
AS
BEGIN
	SELECT *
	FROM [Transaction]
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spTransaction_SxId]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spTransaction_SxId]
GO

CREATE PROCEDURE [dbo].[spTransaction_SxId]
	@IdTransaction nchar(10)
AS
BEGIN
	SELECT *
	FROM [Transaction]
	WHERE [IdTransaction] = @IdTransaction
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spTransaction_I]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spTransaction_I]
GO

CREATE PROCEDURE [dbo].[spTransaction_I]
	@IdTransaction nchar(10),
	@IdUser bigint,
	@DateTime datetime,
	@Status nchar(1),
	@IPAddress nchar(15)
AS
BEGIN
	INSERT INTO [Transaction] ([IdTransaction], [IdUser], [DateTime], [Status], [IPAddress])
	VALUES(@IdTransaction, @IdUser, @DateTime, @Status, @IPAddress)
	
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spTransaction_U]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spTransaction_U]
GO

CREATE PROCEDURE [dbo].[spTransaction_U]
	@IdTransaction nchar(10),
	@IdUser bigint,
	@DateTime datetime,
	@Status nchar(1),
	@IPAddress nchar(15)
AS
BEGIN
	UPDATE [Transaction]
	SET [IdTransaction] = @IdTransaction, [IdUser] = @IdUser, [DateTime] = @DateTime, [Status] = @Status, [IPAddress] = @IPAddress
	WHERE [IdTransaction] = @IdTransaction
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spTransaction_D]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spTransaction_D]
GO

CREATE PROCEDURE [dbo].[spTransaction_D]
	@IdTransaction nchar(10)
AS
BEGIN
	DELETE [Transaction]
	WHERE [IdTransaction] = @IdTransaction
END
GO

------------------------------------
-- TABLE: User
------------------------------------
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spUser_SA]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spUser_SA]
GO

CREATE PROCEDURE [dbo].[spUser_SA]
AS
BEGIN
	SELECT *
	FROM [User]
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spUser_SxEmail]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spUser_SxEmail]
GO

CREATE PROCEDURE [dbo].[spUser_SxEmail]
	@Email bigint
AS
BEGIN
	SELECT *
	FROM [User]
	WHERE [Email] = @Email
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spUser_I]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spUser_I]
GO

CREATE PROCEDURE [dbo].[spUser_I]
	@Email nchar(75),
	@Name nchar(30),
	@LastName nchar(40),
	@ReceiveInfo nchar(75)
AS
BEGIN
	INSERT INTO [User] ([Email], [Name], [LastName], [ReceiveInfo])
	VALUES(@Email, @Name, @LastName, @ReceiveInfo)
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spUser_U]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spUser_U]
GO

CREATE PROCEDURE [dbo].[spUser_U]
	@Email nchar(75),
	@Name nchar(30),
	@LastName nchar(40),
	@ReceiveInfo nchar(75)
AS
BEGIN
	UPDATE [User]
	SET [Email] = @Email, [Name] = @Name, [LastName] = @LastName, [ReceiveInfo] = @ReceiveInfo
	WHERE [Email] = @Email
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spUser_D]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spUser_D]
GO

CREATE PROCEDURE [dbo].[spUser_D]
	@Email nchar(75)
AS
BEGIN
	DELETE [User]
	WHERE [Email] = @Email
END
GO

------------------------------------
-- TABLE: Reservation
------------------------------------
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spReservation_SA]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spReservation_SA]
GO

CREATE PROCEDURE [dbo].[spReservation_SA]
AS
BEGIN
	SELECT *
	FROM [Reservation]
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spReservation_SxId]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spReservation_SxId]
GO

CREATE PROCEDURE [dbo].[spReservation_SxId]
	@IdReservation bigint
AS
BEGIN
	SELECT *
	FROM [Reservation]
	WHERE [IdReservation] = @IdReservation
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spReservation_SxDates]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spReservation_SxDates]
GO

CREATE PROCEDURE [dbo].[spReservation_SxDates]
	@IdParkZone int,
	@StartDate datetime,
	@EndDate datetime
AS
BEGIN
	SELECT *
	FROM [Reservation]
	WHERE IdParkZone = @IdParkZone
		AND DAY([StartDateTime]) >= DAY(@StartDate)
		AND MONTH([StartDateTime]) = MONTH(@StartDate)
		AND YEAR([StartDateTime]) = YEAR(@StartDate)
		AND DAY([EndDateTime]) <= DAY(@EndDate)
		AND MONTH([EndDateTime]) = MONTH(@EndDate)
		AND YEAR([EndDateTime]) = YEAR(@EndDate)
	ORDER BY YEAR([StartDateTime]), MONTH([StartDateTime]), DAY([StartDateTime]), DATEPART(hh, [StartDateTime])
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spReservation_I]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spReservation_I]
GO

CREATE PROCEDURE [dbo].[spReservation_I]
	@IdReservation bigint output,
	@IdParkZone int,
	@IdParkZoneSpace int,
	@Car nchar(10),
	@StartDateTime datetime,
	@EndDateTime datetime,
	@TotalAmount decimal,
	@TotalCommission decimal
AS
BEGIN
	INSERT INTO [Reservation] ([IdParkZone], [IdParkZoneSpace], [Car], [StartDateTime], [EndDateTime], [TotalAmount], [TotalCommission])
	VALUES(@IdParkZone, @IdParkZoneSpace, @Car, @StartDateTime, @EndDateTime, @TotalAmount, @TotalCommission)
	SET @IdReservation = @@IDENTITY
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spReservation_U]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spReservation_U]
GO

CREATE PROCEDURE [dbo].[spReservation_U]
	@IdReservation bigint,
	@IdParkZone int,
	@IdParkZoneSpace int,
	@Car nchar(10),
	@StartDateTime datetime,
	@EndDateTime datetime,
	@TotalAmount decimal,
	@TotalCommission decimal
AS
BEGIN
	UPDATE [Reservation]
	SET [IdParkZone] = @IdParkZone, [IdParkZoneSpace] = @IdParkZoneSpace, [Car] = @Car, [StartDateTime] = @StartDateTime, [EndDateTime] = @EndDateTime, [TotalAmount] = @TotalAmount, [TotalCommission] = @TotalCommission
	WHERE [IdReservation] = @IdReservation
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spReservation_D]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spReservation_D]
GO

CREATE PROCEDURE [dbo].[spReservation_D]
	@IdReservation bigint
AS
BEGIN
	DELETE [Reservation]
	WHERE [IdReservation] = @IdReservation
END
GO

------------------------------------
-- TABLE: TransactionReservation
------------------------------------
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spTransactionReservation_SA]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spTransactionReservation_SA]
GO

CREATE PROCEDURE [dbo].[spTransactionReservation_SA]
AS
BEGIN
	SELECT *
	FROM [TransactionReservation]
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spTransactionReservation_SxId]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spTransactionReservation_SxId]
GO

CREATE PROCEDURE [dbo].[spTransactionReservation_SxId]
	@IdTransaction nchar(10),
	@IdReservation bigint
AS
BEGIN
	SELECT *
	FROM [TransactionReservation]
	WHERE [IdTransaction] = @IdTransaction AND [IdReservation] = @IdReservation
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spTransactionReservation_I]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spTransactionReservation_I]
GO

CREATE PROCEDURE [dbo].[spTransactionReservation_I]
	@IdTransaction nchar(10),
	@IdReservation bigint
AS
BEGIN
	INSERT INTO [TransactionReservation] ([IdTransaction], [IdReservation])
	VALUES(@IdTransaction, @IdReservation)
	
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spTransactionReservation_U]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spTransactionReservation_U]
GO

CREATE PROCEDURE [dbo].[spTransactionReservation_U]
	@IdTransaction nchar(10),
	@IdReservation bigint
AS
BEGIN
	UPDATE [TransactionReservation]
	SET [IdTransaction] = @IdTransaction, [IdReservation] = @IdReservation
	WHERE [IdTransaction] = @IdTransaction AND [IdReservation] = @IdReservation
END
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spTransactionReservation_D]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spTransactionReservation_D]
GO

CREATE PROCEDURE [dbo].[spTransactionReservation_D]
	@IdTransaction nchar(10),
	@IdReservation bigint
AS
BEGIN
	DELETE [TransactionReservation]
	WHERE [IdTransaction] = @IdTransaction AND [IdReservation] = @IdReservation
END
GO
